package com.tan.connection.datatype;

import redis.clients.jedis.Jedis;

import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;

public class RedisHash {
    private static Logger logger = Logger.getLogger(RedisHash.class.getName());

    public static void main(String[] args) {
        Jedis jedis = new Jedis("172.21.50.136",6379);
        jedis.flushDB();
        Map<String,String> map = new HashMap<String, String>();
        map.put("key1","value1");
        map.put("key2","value2");
        map.put("key3","value3");
        map.put("key4","value4");
        //添加名称为hash(key)的hash元素
        jedis.hmset("hash",map);
        //向名称为hash的hash中添加key 为key5,value为value5元素
        jedis.hset("hash","key5","value5");
        logger.info("散列hash的所有键值对为："+jedis.hgetAll("hash"));//return Map<String,String>
        logger.info("散列hash的所有键为："+jedis.hkeys("hash"));//return set<String>
        logger.info("散列hash所有值为："+jedis.hvals("hash"));//return List<String>
        logger.info("将key6保存的值加上一个整数，如果Key6不存在，则添加key6："+jedis.hincrBy("hash","key6",12));
        logger.info("散列hash的所有键值对为："+jedis.hgetAll("hash"));
        logger.info("将key6保存的值加上一个整数，如果key6不存在则添加key6："+jedis.hincrBy("hash","key6",11));
        logger.info("散列hash的所有键值对为："+jedis.hgetAll("hash"));
        logger.info("删除一个或者多个键值对："+jedis.hdel("hash","key2"));
        logger.info("散列hash的所有键值对为："+jedis.hgetAll("hash"));
        logger.info("散列hash的所有键个数："+jedis.hlen("hash"));
        logger.info("判断hash中是否存在Key2："+jedis.hexists("hash","key2"));
        logger.info("判断hash中是否存在Key3："+jedis.hexists("hash","key3"));
        logger.info("获取hash中的值："+jedis.hmget("hash","key3"));
        logger.info("获取hash中的值："+jedis.hmget("hash","key3","key4"));
    }
}
